package com.wyh.aop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 王圆豪
 * Date: 2025-07-17
 * Time: 19:38
 */
@Slf4j
@Component
@Aspect
public class TimeAspect {
    @Pointcut("@annotation(com.wyh.aop.aspect.MyAspect)")
    private void pc(){}

    @Around("pc()")
    public Object recordTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("Around......");
        //记录开始时间
        long start = System.currentTimeMillis();
        //执行目标方法
        Object ret = proceedingJoinPoint.proceed();
        //记录结束时间
        long end = System.currentTimeMillis();
        //记录目标方法执行时间
        log.info(proceedingJoinPoint.getSignature() + "耗时:{}ms", end - start);
        return ret;
    }

    @Before("pc()")
    public void doBefore(){
        log.info("doBefore......");
    }

    @After("pc()")
    public void doAfter(){
        log.info("doAfter......");
    }

    @AfterReturning("pc()")
    public void doAfterReturning(){
        log.info("doAfterReturning......");
    }

    @AfterThrowing("pc()")
    public void doAfterThrowing(){
        log.info("AfterThrowing......");
    }
}
